package logic

import (
	"context"
	"encoding/json"
	"fmt"
	"xialou_platform/app/errorx"
	"xialou_platform/app/tools"
	"xialou_platform/services/agent/api/internal/svc"
	"xialou_platform/services/agent/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type BatchStatisticsLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewBatchStatisticsLogic(ctx context.Context, svcCtx *svc.ServiceContext) BatchStatisticsLogic {
	return BatchStatisticsLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *BatchStatisticsLogic) BatchStatistics(req *types.BatchStatisticsReq) (resp interface{}, err error) {

	// 请求参数
	reqStr, err := json.Marshal(req)
	if err != nil {
		return nil, err
	}

	// 调用休闲吧接口
	bytes, err := tools.RequestOtherPlatform(
		l.svcCtx.Config.XiuXianApi.BatchStatistics,
		"POST",
		l.svcCtx.Config.Auth.AccessSecret,
		string(reqStr),
		l.svcCtx.Config.Auth.AccessExpire,
		10000, // admin_users表 下楼系统账户的ID
	)

	if err != nil {
		return nil, err
	}

	fmt.Println(string(bytes))

	// 解析返回数据
	var res *types.BatchStatisticsResp
	err = json.Unmarshal(bytes, &res)
	if err != nil {
		return nil, errorx.NewMsgReasonCodeError(errorx.DefaultCode, "分页查询批次下的卡详情 err：%v", err.Error())
	}

	return res.Data, err
}
